﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
/// <summary>
/// getsql 的摘要说明
/// </summary>
public class getsql
{
  public static Cache r_cache = HttpContext.Current.Cache;
	public getsql()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    public static string getstr(string conname)
    {
        string constr = "";
        if (conname == "gamestr")
 constr = "server=122.226.254.124,3433;User ID=ten9158room_room_v5;Password=Hh_(_)(@!%^lgs&^25jF;Pooling=true;Max Pool Size=30;database=TenTiange;Connection Reset=FALSE";
          //  constr = "server=60.191.254.162,3433;User ID='ten9158room_web_v2';Password='P@t*5!9Tg';database=TenTiange;Connection Reset=FALSE";
        if (conname == "activestr")
 //constr = "server=122.226.254.124,3433;User ID=ten9158room_room_v5;Password=Hh_(_)(@!%^lgs&^25jF;Pooling=true;Max Pool Size=30;database=TenTiange;Connection Reset=FALSE";
            constr = "server=10.191.232.118,3433;User ID=ten_chamber_main;Password=YH_tenTiange_**_;database=TenTiange;Connection Reset=FALSE";
        if(conname=="Fans9158")
           constr = "server=122.226.254.107,3438;database=FansDB_9158;User ID=FansDB_9158_WebLogin_V3;Password=sX2DC_s.uio3";
       // constr = "server=10.191.231.124,3433;database=FansDB_9158;User ID=FansDB_9158_WebLogin_V3;Password=sX2DC_s.uio3";
        if (conname == "gamestr1")
            constr = "server=122.227.23.112,3433;User ID=tenroom;Password=tenroom;database=TenRoomTest;Connection Reset=FALSE";
        if (conname == "linkstr")
            constr = "60.191.254.169,3433;User ID=Manage9158_V3;Password=ak5t)/IuVaou;database=WebTiange;Connection Reset=FALSE";
        if (conname == "cashString")
            constr = "server=10.191.231.71,3433;User ID=cash_client;Password=p@ssw0rd;database=TiangeCash;Connection Reset=FALSE";
        if (conname == "messtr")
            constr = "server=60.191.254.143,3433;User ID=KTV_9158_V3;Password=HnN>EVM.-Ah[;database=TianGe;Connection Reset=FALSE";
        if (conname == "groupstr")
            constr = "10.191.231.127,3433;User ID='TG_Groups';Password='Groups_**';database='Tg_Groups';Connection Reset=FALSE";
        if (conname == "cardstr")
            constr = "server=10.191.231.139,3433;User ID=vv9158_Count;Password=VV(!%*_MaC88;database=KTVMacStat;Connection Reset=FALSE";
        if(conname=="Fire")
            constr = "server=60.1912.5.4.180,3434;User ID=WebServiceLogin_V3;Password=u2hvYcpv!3Aw;database=fire2db;";
        if (conname == "levelstr")
            constr = "server=60.191.254.145,3433;User ID=lobby_script_V3;Password=)mGuf/?t19ON;database=lobby;Connection Reset=FALSE";
        if (conname == "levelstr1")
            constr = "server=122.227.23.112,3433;User ID=lobby_script;Password=?{f%Aj]^pFVW;database=lobby;Connection Reset=FALSE";
        if (conname == "reg_str2")
            constr = "server=60.191.254.169,3433;User ID=Manage9158_V3;Password=ak5t)/IuVaou;;database=WebTianGe;Connection Reset=FALSE";
        if (conname == "133Tiange")
            constr = "server=60.191.254.169,3433;User ID='Ten9158roomWeb2_V20110906';Password='Esdgs36%^&*246+_sdsd67fd1~c6dh';Pooling=true;Max Pool Size=30;database=Tiange;Connection Reset=FALSE";
        if (conname == "HuoDong")
            constr = "server=122.226.254.102,3433;User ID=actitvity;Password=AjuS^%2!);database=activity;Connection Reset=FALSE";        
        if (conname == "143tiange")
            constr = "server=122.226.254.102,3433;User ID=Statistics;Password=StatK^**@1_^;database=TianGe;Connection Reset=FALSE";  
        if (conname == "searchstr")
            constr = "server=122.226.254.102,3433;User ID=KTVWeb_V1;Password=321ght__*%#_43d;database=Ten_Statitics;Connection Reset=FALSE";
    if (conname == "sql123")
           // constr = "server=10.191.231.123,3433;database=Tiange_newsfeed;User ID=userhome_V1;Password=75gt$%xa$$A6;Connection Reset=FALSE";
         constr = "server=60.191.254.158,3433;database=Tiange_newsfeed;User ID=userhome;Password=M^i0Nt!2Gwx5;Connection Reset=FALSE";//外网
        if (conname == "sql127")
        {
            constr = "server=10.191.231.127,3433;database=Tg_Groups;User ID=TG_GPLogin;Password=dKDEc.~08_+dec:L;Connection Reset=FALSE";
        }
        if (conname == "sql70")
        {
            //constr = "server=10.191.231.70,3433;Database=SEND_MAIL;User Id=SEND_MAIL_DB_USER;Password=gRr5Ln8WSmi*;Connection Reset=FALSE";
             constr = "server=60.191.254.143,3433;Database=SEND_MAIL;User Id=SEND_MAIL_DB_USER;Password=gRr5Ln8WSmi*;Connection Reset=FALSE";//外网
        }
        if (conname == "sql132")
        {
            constr = "server=10.191.231.132,3433;database=WebTiange_Fuzhu;User ID=136_video;Password=br13J_136;Connection Reset=FALSE";
        }
        if (conname == "sql59")
        {
            constr = "server=122.226.254.115,3433;User ID=web_Onstage;Password={{>>>>::5Ok-;database=WeiShow_new;Connection Reset=FALSE";  //外网
            //constr = "server=10.191.231.59,3433;User ID=web_Onstage;Password=D9q@wX0TRak%;database=WeiShow_new;Connection Reset=FALSE";
            //constr = "server=10.191.231.59,3433;User ID=weishow_new;Password=D9q@wX0TRak%;database=WeiShow_new;Connection Reset=FALSE";
        }
        if (conname == "sql147")
        {
            //constr = "server=10.191.231.147,3433;database=hztg-v6cn;User ID=web_chat;Password=PK&Wq&uC;Connection Reset=FALSE";
             constr = "server=122.226.254.100,3433;User ID=wechat;Password=rw:VXo$wIk]N;database=hztg-v6cn;Connection Reset=FALSE"; //外网
        }
	return constr;

 /*       <add key="gamestr" value="server=60.191.254.162,3433;User ID='ten9158room_web_v2';Password='P@t*5!9Tg';database=TenTiange;Connection Reset=FALSE"/>
<add key="activestr" value="server=60.191.254.162,3433;User ID='ten9158room_web_v2';Password='P@t*5!9Tg';database=TenTiange;Connection Reset=FALSE"/>
		<add key="gamestr1" value="server=122.227.23.112,3433;User ID=tenroom;Password=tenroom;database=TenRoomTest;Connection Reset=FALSE" />
<add key="groupstr" value="server=10.191.231.127,3433;User ID='TG_Groups';Password='Groups_**';database='Tg_Groups';Connection Reset=FALSE"/>
<add key="linkstr" value="server=10.191.231.134,3433;User ID=Manage9158_V3;Password=ak5t)/IuVaou;database=WebTiange;Connection Reset=FALSE"/>
<add key="cashString" value="server=10.191.231.71,3433;User ID=cash_client;Password=p@ssw0rd;database=TiangeCash;Connection Reset=FALSE" />
<add key="messtr" value="server=10.191.231.70,3433;User ID=KTV_9158_V1;Password=t$i4HcXiXC~n;database=TianGe;Connection Reset=FALSE"/>	
 <add key="cardstr" value="server=10.191.231.139,3433;User ID=vv9158_Count;Password=VV(!%*_MaC88;database=KTVMacStat;Connection Reset=FALSE"/>
   <add key="Fire" value="server=10.191.231.249,3434;User ID=WebServiceLogin_V3;Password=u2hvYcpv!3Aw;database=fire2db;"/>
<add key="levelstr" value="server=10.191.231.82,3433;User ID=lobby_script_V3;Password=)mGuf/?t19ON;database=lobby;Connection Reset=FALSE" />
<add key="levelstr1" value="server=122.227.23.112,3433;User ID=lobby_script;Password=?{f%Aj]^pFVW;database=lobby;Connection Reset=FALSE" />
 <add key="reg_str2" value="server=10.191.231.134,3433;User ID=Manage9158_V3;Password=ak5t)/IuVaou;;database=WebTianGe;Connection Reset=FALSE"/>
	</appSettings>*/
    }

    public static string JsonString(string str)
    {
        if (str.Length > 0)
        {
            str = str.Replace("\\", "\\\\");
            str = str.Replace("/", "\\/");
            str = str.Replace("'", "\\'");
            str = str.Replace("\"", "\\\"");
            str = str.Replace("\r\n", "\\n");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace(Convert.ToChar(10).ToString(), "\\n");
            str = Regex.Replace(str, @"[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]", "", RegexOptions.IgnoreCase);
        }
        return str;
    }
 public static string GetIP(HttpRequest Rq)
    {
        string ip = "";
        try
        {
            if (Rq.Headers["Cdn-Src-Ip"] != null && Rq.Headers["Cdn-Src-Ip"] != String.Empty)
                ip = Rq.Headers["Cdn-Src-Ip"];
            else if (Rq.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && Rq.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty)
                ip = Rq.ServerVariables["HTTP_X_FORWARDED_FOR"];
            else
                ip = Rq.UserHostAddress;
            //ip = (Rq.Headers["Cdn-Src-Ip"] != null && Rq.Headers["Cdn-Src-Ip"] != String.Empty) ? Rq.Headers["Cdn-Src-Ip"] : Rq.UserHostAddress;

            if (ip == "")
            {
                ip = Rq.ServerVariables["REMOTE_ADDR"];
            }
            else if (ip.IndexOf(',') != -1)
            {
                ip = ip.Split(',')[0];
            }
        }
        catch
        {
            return "";
        }
        return ip;
    }

    #region 进房类型
    public static string getipstr(string serverid)
    {

        string statstr = "0";
        DataTable metable = getpart();
        if (metable != null)
        {
            //				DataRow[] merow = metable.Select("idx=" + serverid + " and mipname='同城有缘'");
            //				if (merow.Length > 0)
            //					statstr = "1";
            //				merow = metable.Select("idx=" + serverid + " and mipname='演艺天地'");
            //				if (merow.Length > 0)
            //					statstr = "2";
            //				merow = metable.Select("idx=" + serverid + " and mipname='美女聊吧'");
            //				if (merow.Length > 0)
            //					statstr = "3";
            DataRow[] merow = metable.Select("idx=" + serverid);
            if (merow.Length > 0)
                statstr = merow[0]["partsort"].ToString();
        }
        return statstr;
    }

    public static DataTable getpart()
    {
        string connstr = getsql.getstr("gamestr");
        DataTable metable = null;
        SqlParameter[] p =
				 {
                    
				 };
        if (r_cache["partinfo"] == null)
        {
            metable = SqlHelper.ExecuteDataTable(connstr, CommandType.StoredProcedure, "f_ktvpart_intro", p);
            r_cache.Insert("partinfo", metable, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
        }
        else
        {
            metable = (DataTable)r_cache["partinfo"];
        }
        return metable;
    }
    #endregion

    public static DataTable getroom()
    {
        DataTable metable = null;
        if (r_cache["roomlist"] == null)
        {
            SqlParameter[] p =
                 {
                    
                 };

            metable = SqlHelper.ExecuteDataTable(getsql.getstr("gamestr"), CommandType.StoredProcedure, "[dbo].[f_multiroom_list_test]", p);
            r_cache.Insert("roomlist", metable, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero);
        }
        else
        {
            metable = (DataTable)r_cache["roomlist"];
        }
        return metable;
    }

    #region 百人房进入
    public static string roomin_mul(int roomid)
    {
        string function = "";
        string strFilter = "idx=" + roomid;

        DataTable dt = null;
        dt =getroom();
        DataRow[] filteredRows;
        filteredRows = dt.Select(strFilter);
        if (filteredRows.Length < 1)
        {
            return function;

            //				SqlParameter[] p=
            //							{
            //
            //								
            //							};
            //				dt=SqlHelper.ExecuteDataTable(User_Manager.Class.sqldb.Get_constr("TenRoomes.DataAccess.ConnectionString"), CommandType.StoredProcedure, "f_multiroom_list_test", p);
            //		//		dt=SqlHelper.ExecuteDataTable(ConfigurationSettings.AppSettings["TenRoomes.DataAccess.ConnectionString"], CommandType.StoredProcedure, "f_multiroom_list_test", p);
            //				filteredRows =dt.Select(strFilter);
        }
        int ntype = int.Parse(filteredRows[0]["type"].ToString().Trim());
        string id = filteredRows[0]["idx"].ToString().Trim();
        string name = filteredRows[0]["name"].ToString().Trim();
        string ip = filteredRows[0]["ip"].ToString().Trim();
        string chatport = filteredRows[0]["chatport"].ToString().Trim();
        ;
        string audioport = filteredRows[0]["audioport"].ToString().Trim();
        string ktvport = filteredRows[0]["ktvport"].ToString().Trim();
        //			string videoip	=filteredRows[0]["videoip"].ToString().Trim();
        //			string videoport=filteredRows[0]["videoport"].ToString().Trim();
        string videoip = filteredRows[0]["tmtip"].ToString().Trim();
        string videoport = filteredRows[0]["tmtport"].ToString().Trim();
        string cncip = filteredRows[0]["cncip"].ToString().Trim();
        string cncport = filteredRows[0]["cncport"].ToString().Trim();
        string servertype = "1";
        servertype = System.Configuration.ConfigurationSettings.AppSettings["servertype"];
        
        //-----------------------------------------多人进入

        string    userid = "9999";
        ktvport = userid;
        //---------------------------------------------------
        string partip = filteredRows[0]["partip"].ToString().Trim();
        if (servertype == "0")
        {
            cncip = videoip;
            cncport = videoport;

        }
        else
        {
            if (cncip == "")
            {
                cncip = videoip;
                cncport = videoport;
            }
            if (partip != "")
                ip = partip;
        }
        cncip = getipstr(filteredRows[0]["servertypeidx"].ToString().Trim());
        if (ntype == 1)
        {
            function += "start('CLASS_EPH_PTS_CHAT_3/25/2004 9:57','" + id + "','" + name + "','" + ip + "','" + chatport + "','" + videoport + "','" + audioport + "','" + ntype + "','','0','2') ";
        }
        else if (ntype == 2)
        {
            function += "startktv('CLASS_EPH_PTS_CHAT_3/25/2004 9:57','" + id + "','" + name + "','" + ip + "','" + chatport + "','" + videoport + "','" + audioport + "','" + ntype + "','','0','2','" + ktvport + "')";
        }
        else if (ntype == 5)
        {
            //	function+="startGuest('CLASS_EPH_PTS_CHAT_3/25/2004 9:57','"+id+"','"+name+"','"+ip+"','"+chatport+"','"+videoport+"','"+audioport+"','"+ntype+"','','0','2','"+ktvport+"','"+videoip+"') ";
            function += "startGuest('CLASS_EPH_PTS_CHAT_3/25/2004 9:57','" + id + "','" + name + "','" + ip + "','" + chatport + "','" + cncport + "','" + audioport + "','" + ntype + "','','0','2','" + ktvport + "','" + cncip + "') ";
        }
        else
        {
            //	function+="startVIP('CLASS_EPH_PTS_CHAT_3/25/2004 9:57','"+id+"','"+name+"','"+ip+"','"+chatport+"','"+videoport+"','"+audioport+"','"+ntype+"','','0','2','"+ktvport+"','"+videoip+"')";
            function += "startVIP('CLASS_EPH_PTS_CHAT_3/25/2004 9:57','" + id + "','" + name + "','" + ip + "','" + chatport + "','" + cncport + "','" + audioport + "','" + ntype + "','','0','2','" + ktvport + "','" + cncip + "')";
        }

        return function;
    }

    #endregion

    #region 比较用人气
    public static DataTable get_oldhoto()
    {
        DataTable metable = null;
        SqlParameter[] p =
                 {
                 };

        //   metable = SqlHelper.ExecuteDataTable(FlashGameDBConnstring, CommandType.StoredProcedure, "[dbo].[f_partonline_list]", p);
        if (r_cache["partonline_oldo"] == null)
        {
            metable = SqlHelper.ExecuteDataTable(getstr("searchstr"), CommandType.StoredProcedure, "[dbo].[f_getpart_oldlist]", p);
            r_cache.Insert("partonline_oldo", metable, null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);
        }
        else
        {
            metable = (DataTable)r_cache["partonline_oldo"];
        }
        return metable;
    }
    #endregion
    #region 比较用消费
    public static DataTable get_oldhotc()
    {
        DataTable metable = null;
        SqlParameter[] p =
                 {
                 };

        //   metable = SqlHelper.ExecuteDataTable(FlashGameDBConnstring, CommandType.StoredProcedure, "[dbo].[f_partonline_list]", p);
        if (r_cache["partonline_oldc"] == null)
        {
            metable = SqlHelper.ExecuteDataTable(getstr("gamestr"), CommandType.StoredProcedure, "[dbo].[f_getpart_oldlist]", p);
            r_cache.Insert("partonline_oldc", metable, null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);
        }
        else
        {
            metable = (DataTable)r_cache["partonline_oldc"];
        }
        return metable;
    }
    #endregion
}
